//
//  ShareView.m
//  Credit
//
//  Created by WangZhenyu on 16/6/14.
//  Copyright © 2016年 WangZhenyu. All rights reserved.
//

#import "ShareView.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <ShareSDKExtension/ShareSDK+Extension.h>
#import "AppDelegate.h"

@interface ShareView(){
    NSString *urlStr;
    NSString *contentStr;
}
@property (weak, nonatomic) IBOutlet UIButton *qqBtn;
@property (weak, nonatomic) IBOutlet UIButton *weiboBtn;
@property (weak, nonatomic) IBOutlet UIButton *pengyouquanBtn;
@property (weak, nonatomic) IBOutlet UIButton *weixinBtn;

@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation ShareView

- (id)initItemContent:(NSString *)content{
    self = [[[NSBundle mainBundle] loadNibNamed:@"ShareView" owner:self options:nil] lastObject];
    if (self) {
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE)];
        [self.bottomView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE, SCREEN_WIDTH_DEVICE, 232 + IS_iPhoneX_Bottom)];
        [UIView animateWithDuration:0.3 animations:^{
            [self.bottomView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE - 232 - IS_iPhoneX_Bottom, SCREEN_WIDTH_DEVICE, 232 + IS_iPhoneX_Bottom)];
        }];
        contentStr = @"1分钟申请，最快10分钟拿钱，解您燃眉之急";
        if (content) {
            contentStr = content;
        }
        urlStr = nil;
        self.qqBtn.enabled = [ShareSDK isClientInstalled:SSDKPlatformSubTypeQQFriend];
        self.pengyouquanBtn.enabled = [ShareSDK isClientInstalled:SSDKPlatformSubTypeWechatSession];
        self.weixinBtn.enabled = [ShareSDK isClientInstalled:SSDKPlatformSubTypeWechatSession];
    }
    return self;
}

- (id)initItemContent:(NSString *)content withUrl:(NSString *)url{
    self = [[[NSBundle mainBundle] loadNibNamed:@"ShareView" owner:self options:nil] lastObject];
    if (self) {
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE)];
        [self.bottomView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE, SCREEN_WIDTH_DEVICE, 232 + IS_iPhoneX_Bottom)];
        [UIView animateWithDuration:0.3 animations:^{
            [self.bottomView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE - 232 - IS_iPhoneX_Bottom, SCREEN_WIDTH_DEVICE, 232 + IS_iPhoneX_Bottom)];
        }];
        contentStr = @"1分钟申请，最快10分钟拿钱，解您燃眉之急";
        if (content) {
            contentStr = content;
        }
        urlStr = SHAREAPPURL;
        if (url) {
            urlStr = url;
        }
        self.qqBtn.enabled = [ShareSDK isClientInstalled:SSDKPlatformSubTypeQQFriend];
        self.pengyouquanBtn.enabled = [ShareSDK isClientInstalled:SSDKPlatformSubTypeWechatSession];
        self.weixinBtn.enabled = [ShareSDK isClientInstalled:SSDKPlatformSubTypeWechatSession];
    }
    return self;
}

- (IBAction)removePress {
    [UIView animateWithDuration:0.2 animations:^{
        [self.bottomView setFrame:CGRectMake(0, SCREEN_HEIGHT_DEVICE, SCREEN_WIDTH_DEVICE, 232)];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (IBAction)qqPress {
//    if ([ShareSDK isClientInstalled:SSDKPlatformSubTypeQQFriend]) {
        [self shareType:SSDKPlatformSubTypeQQFriend];
//    }else{
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                            message:@"手机未安装qq客户端，无法分享"
//                                                           delegate:nil
//                                                  cancelButtonTitle:@"确定"
//                                                  otherButtonTitles:nil];
//        [alertView show];
//    }
}

- (IBAction)weiboPress {
//    if ([ShareSDK isClientInstalled:SSDKPlatformTypeSinaWeibo]) {
        [self shareType:SSDKPlatformTypeSinaWeibo];
//    }else{
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                            message:@"手机未安装新浪微博客户端，无法分享"
//                                                           delegate:nil
//                                                  cancelButtonTitle:@"确定"
//                                                  otherButtonTitles:nil];
//        [alertView show];
//    }
    
}

- (IBAction)pengyouquanPress {
//    if ([ShareSDK isClientInstalled:SSDKPlatformSubTypeWechatSession]) {
        [self shareType:SSDKPlatformSubTypeWechatTimeline];
//    }else{
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                            message:@"手机未安装微信客户端，无法分享"
//                                                           delegate:nil
//                                                  cancelButtonTitle:@"确定"
//                                                  otherButtonTitles:nil];
//        [alertView show];
//    }
}

- (IBAction)weixinhaoyouPress {
//    if ([ShareSDK isClientInstalled:SSDKPlatformSubTypeWechatSession]) {
        [self shareType:SSDKPlatformSubTypeWechatSession];
//    }else{
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                            message:@"手机未安装微信客户端，无法分享"
//                                                           delegate:nil
//                                                  cancelButtonTitle:@"确定"
//                                                  otherButtonTitles:nil];
//        [alertView show];
//    }
}

- (void)shareType:(SSDKPlatformType)type{
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    if (type == SSDKPlatformSubTypeWechatTimeline) {
        [shareParams SSDKSetupShareParamsByText:nil
                                         images:@[[UIImage imageNamed:@"shareicon"]]
                                            url:[NSURL URLWithString:urlStr]
                                          title:contentStr
                                           type:SSDKContentTypeAuto];
    }else if (type == SSDKPlatformTypeSinaWeibo){
        [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@%@", contentStr, urlStr]
                                         images:@[[UIImage imageNamed:@"shareicon"]]
                                            url:[NSURL URLWithString:urlStr]
                                          title:@"霸王贷信息服务平台"
                                           type:SSDKContentTypeAuto];
    }else{
        [shareParams SSDKSetupShareParamsByText:contentStr
                                         images:@[[UIImage imageNamed:@"shareicon"]]
                                            url:[NSURL URLWithString:urlStr]
                                          title:@"霸王贷信息服务平台"
                                           type:SSDKContentTypeAuto];
    }
    
    
    
    //2、分享（可以弹出我们的分享菜单和编辑界面）
    [ShareSDK share:type parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        NSLog(@"%ld",(long)error.code);
        switch (state) {
            case SSDKResponseStateSuccess:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                    message:nil
                                                                   delegate:nil
                                                          cancelButtonTitle:@"确定"
                                                          otherButtonTitles:nil];
                [alertView show];
                [self removeFromSuperview];
                break;
            }
            case SSDKResponseStateFail:
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                message:nil
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil, nil];
                [alert show];
                [self removeFromSuperview];
                break;
            }
            default:
                [self removeFromSuperview];
                break;
        }
    }];
}
- (IBAction)copyPress {
    [self removePress];
    [self OnCopyLink];
}

- (void)OnCopyLink
{
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    //pasteboard.string = [NSString stringWithFormat:@"%@ %@",strContent,urlString];
    pasteboard.string = urlStr;
    AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [del.navigationController.topViewController showHUDAlert:@"复制成功，粘贴分享给好友"];
}

@end
